package com.imooc.web;

import com.imooc.mapper.ItemsSpecMapperCustom;
import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.service.ItemService;
import com.imooc.utils.PagedResult;
import com.imooc.utils.Result;
import com.imooc.vo.CommentLevelCountsVo;
import com.imooc.vo.ItemInfoVo;
import com.imooc.vo.ShopcartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <h1></h1>
 *
 * @author : dzj
 * @create: 2022-02-17 18:24
 */
@Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
@RestController
@RequestMapping("/items")
public class ItemsController extends BaseController {
    @Autowired
    private ItemService itemService;


    @ApiOperation(value = "查询商品详情",notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public Result info(@PathVariable(value = "itemId") String itemId){
        if(StringUtils.isBlank(itemId)) {
            return Result.errorMsg("需要商品编号");
        }
        Items item = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgs = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecs = itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);
        ItemInfoVo itemInfoVo = new ItemInfoVo(item, itemsImgs, itemsSpecs, itemsParam);


        return Result.ok(itemInfoVo);
    }
    @ApiOperation(value = "查询评价统计信息", notes = "查询评价统计信息" , httpMethod = "GET")
    @GetMapping("/commentLevel")
    public Result commentLevel(@RequestParam(required = true) @ApiParam(value = "商品id",required = true) String itemId) {
        CommentLevelCountsVo commentLevelCountsVo = itemService.queryCommentCounts(itemId);
        return Result.ok(commentLevelCountsVo);
    }
    @ApiOperation(value = "查询商品评论", notes = "查询商品评论" , httpMethod = "GET")
    @GetMapping("/comments")
    public Result comments(
           @ApiParam(name = "level",value = "评价等级",required = false) @RequestParam(required = false) Integer level,
           @ApiParam(name = "itemId",value = "商品编号",required = true) @RequestParam  String itemId,
           @ApiParam(name = "page",value = "页码",required = false) @RequestParam(required = false)   Integer page,
           @ApiParam(name = "pageSize",value = "页容量",required = false) @RequestParam(required = false)   Integer pageSize) {
        if(StringUtils.isBlank(itemId)) {
            return Result.errorMsg("商品编号不得为空");
        }
        if(page==null) {
            page= 1;
        }
        if(pageSize==null) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        return Result.ok(itemService.queryPagedComments(itemId,level,page,pageSize));
    }

    /**
     *
     * @param keyWords
     * @param sort  k:keyword, p:price, c:sellCount  注意 choose里面  ' 转义 &quot; 问题
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "搜索商品列表", notes = "搜索商品列表" , httpMethod = "GET")
    @GetMapping("/search")
    public Result search(
            @ApiParam(name = "keywords",value = "搜索关键词",required = true) @RequestParam(required = true)  String keywords,
            @ApiParam(name = "sort",value = "排序",required = false) @RequestParam(required = false)  String sort,
            @ApiParam(name = "page",value = "页码",required = false) @RequestParam(required = false)  Integer page,
            @ApiParam(name = "pageSize",value = "页容量",required = false) @RequestParam(required = false)  Integer pageSize) {
        if(StringUtils.isBlank(keywords)) {
            return  Result.errorMsg("搜索关键词不能为空");
        }
        if(page==null) {
            page= 1;
        }
        if(pageSize==null) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedResult pagedResult ;

             pagedResult = itemService.searchItems(keywords, sort, page, pageSize);


        return Result.ok(pagedResult);
    }
    /**
     *
     * @param catId
     * @param sort  k:keyword, p:price, c:sellCount  注意 choose里面  ' 转义 &quot; 问题
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "通过分类编号搜索商品列表", notes = "通过分类编号搜索商品列表" , httpMethod = "GET")
    @GetMapping("/catItems")
    public Result catItems(
            @ApiParam(name = "catId",value = "三级分类编号",required = true) @RequestParam(required = true)  Integer catId,
            @ApiParam(name = "sort",value = "排序",required = false) @RequestParam(required = false)  String sort,
            @ApiParam(name = "page",value = "页码",required = false) @RequestParam(required = false)  Integer page,
            @ApiParam(name = "pageSize",value = "页容量",required = false) @RequestParam(required = false)  Integer pageSize) {
        if(catId==null) {
            return  Result.errorMsg("分类不能为空");
        }
        if(page==null) {
            page= 1;
        }
        if(pageSize==null) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedResult pagedResult = itemService.searchItemsByThirdCat(catId, sort, page, pageSize);

        return Result.ok(pagedResult);
    }
// 用户用户长时间未登陆网址, 刷新购物车中的数据, 主要是商品价格
    @ApiOperation(value = "根据商品规格ids查找最新的商品数据", notes = "根据商品规格ids查找最新的商品数据" , httpMethod = "GET")
    @GetMapping("/refresh")
    public Result refresh(
            @ApiParam(name = "itemSpecIds",value = "拼接的规格id",required = true,example = "1001,1003") @RequestParam(required = true)  String itemSpecIds
            ) {
       if(StringUtils.isBlank(itemSpecIds)) {
           return Result.ok();
       }
        List<ShopcartVo> shopcatVos = itemService.queryItemsBySpecIds(itemSpecIds);

        return Result.ok(shopcatVos);
    }
}
